package com.lanou.renrenxin.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.renrenxin.R;
import com.lanou.renrenxin.base.BaseActivity;
import com.parse.GetCallback;
import com.parse.GetDataCallback;
import com.parse.ParseAnalytics;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;

/**
 * Created by zouhui on 16/3/5.
 * 个人信息界面
 */
public class PIMActivity extends BaseActivity implements View.OnClickListener {
    private TextView TitleTv,nickNameTv;
    private Button btnBack,btnStartChat;
    private String nickName;
    private String userName;
    private Object uSerPhoto;
    private ImageView imageViewUserPhoto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pim);

        ParseAnalytics.trackAppOpenedInBackground(getIntent());
        findId();
        loadData();
    }


    private void findId() {
        TitleTv = (TextView) findViewById(R.id.txtTitle);
        btnBack = (Button) findViewById(R.id.btnBack);
        btnStartChat = (Button) findViewById(R.id.btn_startchat);
        nickNameTv = (TextView) findViewById(R.id.pim_tv_nickname);
        imageViewUserPhoto = (ImageView) findViewById(R.id.contactdetail_photo_iv);
    }

    private void loadData() {
        Intent intent = getIntent();
        nickName = intent.getStringExtra("detail");
        userName = intent.getStringExtra("PIM_USER_NAME");
        nickNameTv.setText(nickName);
        TitleTv.setText("详细信息");
        btnBack.setVisibility(View.VISIBLE);
        btnBack.setOnClickListener(this);
        btnStartChat.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnBack:
                finish();
                break;
            //开始聊天
            case R.id.btn_startchat:
                Intent intent = new Intent(this,ChattingActivity.class);
                intent.putExtra("M_USER_NAME",userName);
                intent.putExtra("USER_NAME",nickName);
                startActivity(intent);
                finish();
                break;
        }
    }

    // TODO: 16/3/8 获取联系人头像
    public Object getDataWithKey() {
        //表明
        ParseQuery<ParseObject> pq = ParseQuery.getQuery("ChatRecording");
//        Log.d("PIMActivity", ParseUser.getCurrentUser().getObjectId());
        pq.getInBackground("brpv4D8AIk", new GetCallback<ParseObject>() {
            @Override
            public void done(ParseObject object, ParseException e) {
                e.printStackTrace();
                //列名
                ParseFile file = object.getParseFile("head_image");


                file.getDataInBackground(new GetDataCallback() {
                    @Override
                    public void done(byte[] data, ParseException e) {
                        Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);

                        imageViewUserPhoto.setImageBitmap(bmp);
                    }
                });
            }
        });


        return uSerPhoto;
    }

}
